package
{
    import flash.events.MouseEvent;
    import flash.geom.Point;
    
    import grail.RatingEvent;
    
    import mx.controls.Image;
    import mx.controls.listClasses.BaseListData;
    import mx.controls.listClasses.IDropInListItemRenderer;
    import mx.controls.listClasses.IListItemRenderer;
    import mx.core.IDataRenderer;
    import mx.core.UIComponent;
    import mx.events.FlexEvent;
    
    [Event(name="rating", type="grail.RatingEvent")]
    public class StarRatingRenderer6 extends UIComponent implements IDropInListItemRenderer, 
        IListItemRenderer, IDataRenderer
    {
        [Embed(source="images/red_star.gif")]
        private static var red_star:Class;
        
        [Embed(source="images/yellow_star.gif")]
        private static var yellow_star:Class;
        
        [Embed(source="images/white_star.gif")]
        private static var white_star:Class;
        
        private var images:Array = [];
        public var myStarRating:int = 0;
        private var mousedOverStar:int = -1;
        
        public function StarRatingRenderer6()
        {
            super();
            
            addEventListener("mouseMove", mouseMoveHandler);
            addEventListener("click", clickHandler);
            addEventListener("rollOut", rollOutHandler);
        }
        
        override protected function createChildren():void
        {
            for (var i:int = 0; i < 5; i++)
            {
                images[i] = new Image();
                images[i].source = white_star;
                this.addChild(images[i]);
            }
        }
        
        override protected function measure():void
        {
            super.measure();
            
            measuredWidth = 5 * 17;
            measuredHeight = 15; 
        }
        
        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
        {
            super.updateDisplayList(unscaledWidth, unscaledHeight);
            
            // we are ignoring the width and height
            
            // if mousedOverStar = -1, use myStarRating and yellow and white stars
            // if mousedOverStar >= 0, use mousedOverStar and red and white stars
            for (var i:int = 0; i < 5; i++)
            {
                if (mousedOverStar == -1)
                {
                    if (myStarRating > i)
                        images[i].source = yellow_star;
                    else
                        images[i].source = white_star;
                }
                else
                {
                    if (mousedOverStar > i)
                        images[i].source = red_star;
                    else
                        images[i].source = white_star;
                }
                
                images[i].setActualSize(17, 15);
                images[i].move(17*i, 0);
            }
        }
        
        private var _data:Object;
        
        [Bindable("dataChange")]
        public function get data():Object
        {
            return _data;
        }
        
        public function set data(value:Object):void
        {
            _data = value;
            myStarRating = parseInt(listData.label);
            
            dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));

            invalidateDisplayList();
        }
        
        private var _listData:BaseListData;
        
        public function get listData():BaseListData
        {
            return _listData;
        }
        
        public function set listData(value:BaseListData):void
        {
            _listData = value;
        }
        
        private function mouseMoveHandler(event:MouseEvent):void
        {
            var local:Point = globalToLocal(new Point(event.stageX, event.stageY));
            mousedOverStar = pointToStar(local);
            
            invalidateDisplayList();
        }
        
        private function pointToStar(point:Point):int
        {
            var starIndex:int = 0;
               
            // highlight them all as red up to this point
            for (var i:int = 0; i < images.length; i++)
            {
                // use width/4 so first 1/4 of the image doesn't count.
                // this gives some leeway and allows them to give someone a rating of 0.
                if (point.x >= images[i].x + images[i].width/4)
                    starIndex++;
                else
                    break;
            }
            
            return starIndex;
        }
        
        private function clickHandler(event:MouseEvent):void
        {
        	var ratingEvent:RatingEvent;
            var local:Point = globalToLocal(new Point(event.stageX, event.stageY));
            
            mousedOverStar = -1;
            myStarRating = pointToStar(local);
            
            ratingEvent = new RatingEvent;
            ratingEvent.track_id = _data.track_id;
            ratingEvent.rating = myStarRating;
            
            dispatchEvent( ratingEvent ); 
            
            invalidateDisplayList();
        }
        
        private function rollOutHandler(event:MouseEvent):void
        {
            mousedOverStar = -1;
            
            invalidateDisplayList();
        }
        
        private function storeRating():void {
        	
        	
        	
        }
        
    }
}